let channelId;
layui.extend({
    zmnExportUtil: '{/}/static/js/zmn.excel.util'
}).define(['index', 'form', 'table', 'laypage', 'layedit', 'laydate', 'zmnExportUtil'], function (exports) {
    let $ = layui.$;
    let layer = layui.layer;
    let page = layui.laypage;
    let table = layui.table;
    let form = layui.form;
    let zmnExportUtil = layui.zmnExportUtil;
    channelId = json.channelId;

    table.init('zmn-table', {
        toolbar: "#zmn-table-tool",
        page: true,
        limit: json.pageSize,
        height: 'full-250',
        done: function () {
            $("div[lay-event='LAYTABLE_EXPORT']").attr('lay-event', 'CUSTOM_EXPORT');
        }
    });

    page.render(zmnPage.laypageOptions(json));

    zmnProduct.laySelectSpecial(form, $, "/product/listChilds.action",
        "/product/listCateChilds.action",
        "servCategId",
        "categOneId",
        "categId");

    table.on('tool(zmn-table)', function (obj) {
        if (obj.event === 'mappingChannelProduct'){
            mappingChannelProduct(obj.data.productId);
        }
    });

    table.on("toolbar(zmn-table)", function (obj) {
        //导出excel
        if(obj.event=='CUSTOM_EXPORT'){
            zmnExportUtil.doExportExcel('zmn-table','服务产品');
        }

        let items = obj.config.data;
        let ids = zmnPage.layIdsChecked(items, "productId");
        if (obj.event != "start" && obj.event != "stop") {
            return;
        }
        if (ids.length === 0) {
            return layer.alert("请选择至少1个产品！");
        }
        let url = "/channel/servProduct/updateStatus.action";
        switch (obj.event) {
            case 'stop':
                layer.confirm('确定停用吗？', function () {
                    zmnPage.ajaxSimple(url, {ids: ids.join(","), status: 1, channelId: channelId}, 1000);
                });
                break;
            case 'start':
                zmnPage.ajaxSimple(url, {ids: ids.join(","), status: 2, channelId: channelId}, 1000);
                break;
        }
    });

    let mappingChannelProduct = function (productId) {
        zmnLayUtil.openIframeCustom(layer, "/channel/mappingChannelProduct.action?productId=" + productId + "&channelId=" + json.channelId, {
            title: "添加映射关系",
            shade: 0.8,
            area:["550px", "300px"]
        })
    };

    exports('channel_servproduct_list', {});
});


let zmnProduct = {

    laySelectSpecial: function (form, $, url1, url2, selectOneId, selectTwoId, selectThrId) {
        let servCategId;
        let categOneId;
        let servType = '<option value="">-服务分类-</option>';
        form.on("select(showType)", function (obj) {
            servType = '<option value="">-服务分类-</option>';
            let showType = $('#showType option:selected').val();
            if (showType == 2){
                url1 = '/showProduct/listChilds.action';
                url2 = '/showProduct/listCateChilds.action';
            } else {
                url1 = '/product/listChilds.action';
                url2 = '/product/listCateChilds.action';
            }
            $.ajax({
                type: "GET", url: "/channel/loadServByShowType.action",
                dataType: "json", data: {
                    showType: obj.value,
                    channelId: channelId
                },
                success: function (items) {
                    console.log(items);
                    items.forEach(item => {
                        servType += '<option value="' + item.value + '">' + item.text + '</option>';
                    });
                    $("#servCategId").html(servType);
                    let oneLevelType = '<option value="">-1级分类-</option>';
                    let twoLevelType = '<option value="">-2级分类-</option>';
                    $("#" + selectTwoId).html(oneLevelType);
                    $("#" + selectThrId).html(twoLevelType);
                    form.render("select");
                }
            });
        });

        form.on("select(" + selectOneId + ")", function (obj) {
            if (zmnUtil.isNull(obj) || obj.value === 0) {
                return;
            }
            servCategId = obj.value;
            let showType = $('#showType option:selected').val();
            if (showType == 2){
                url1 = '/showProduct/listChilds.action';
                url2 = '/showProduct/listCateChilds.action';
            } else {
                url1 = '/product/listChilds.action';
                url2 = '/product/listCateChilds.action';
            }
            let oneLevelType = '<option value="">-1级分类-</option>';
            let twoLevelType = '<option value="">-2级分类-</option>';
            $.ajax({
                type: "GET", url: url1,
                dataType: "json", data: {parentId: servCategId},
                success: function (items) {
                    items.forEach(item => {
                        oneLevelType += '<option value="' + item[0] + '">' + item[1] + '</option>';
                    });
                    $("#" + selectTwoId).html(oneLevelType);
                    $("#" + selectThrId).html(twoLevelType);
                    form.render("select");
                }
            });
        });
        if (zmnUtil.isEmptyNull(selectThrId)) {
            return;
        }
        form.on("select(" + selectTwoId + ")", function (obj) {
            if (zmnUtil.isNull(obj) || obj.value === 0) {
                return;
            }
            categOneId = obj.value;
            servCategId = $('#servCategId option:selected').val();
            let showType = $('#showType option:selected').val();
            if (showType == 2){
                url1 = '/showProduct/listChilds.action';
                url2 = '/showProduct/listCateChilds.action';
            } else {
                url1 = '/product/listChilds.action';
                url2 = '/product/listCateChilds.action';
            }
            let twoLevelType = '<option value="">-2级分类-</option>';
            $.ajax({
                type: "GET", url: url2,
                dataType: "json", data: {categOneId: obj.value, servCategId: servCategId},
                success: function (items) {
                    items.forEach(item => {
                        twoLevelType += '<option value="' + item[0] + '">' + item[1] + '</option>';
                    });
                    $("#" + selectThrId).html(twoLevelType);
                    form.render("select");
                }
            });
        });
    },
};